# 0.0 Initialize ----
source("src/initialize.r")

# 1.0 DV = Perceived Effectiveness ----
# 1.1 Figure 1 ----
ds %>% 
  drop_na(effective) %>%
  group_by(pos_treatment) %>% 
  summarise(
    nrow = length(effective), 
    mean = mean(effective), 
    se = sd(effective)/sqrt(nrow),
    ci = 1.96 * se,
    lower_ci = mean - ci,
    upper_ci = mean + ci
  ) %>% 
  ggplot(., aes(x = pos_treatment, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 1, 
    linewidth = 1
  ) + 
  labs(y = "Effectiveness Rating (1-7)", title = "") +  
  scale_x_discrete(
    labels = c(
      "control" = "Control \n", 
      "nocom" = "No \nCommittee", 
      "commem" = "Subcommittee\nMember",
      "comlead" = "Subcommittee\nLeader"
    )
  ) + 
  scale_y_continuous(limits = c(4, 5.5))

ds %>% 
  drop_na(money) %>%
  group_by(pos_treatment) %>% 
  summarise(
    nrow = length(money), 
    mean = mean(money), 
    se = sd(money)/sqrt(nrow),
    ci = 1.96 * se,
    lower_ci = mean - ci,
    upper_ci = mean + ci
  ) %>% 
  ggplot(., aes(x = pos_treatment, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean-ci, 
      ymax = mean+ci
    ), 
    position = position_dodge(width = .5), 
    size = 1, 
    linewidth = 1
  ) + 
  labs(y = "Effectiveness Rating (1-7)", title = "") +  
  scale_x_discrete(
    labels = c(
      "control" = "Control \n", 
      "nocom" = "No \nCommittee", 
      "commem" = "Subcommittee\nMember",
      "comlead" = "Subcommittee\nLeader"
    )
  ) + 
  scale_y_continuous(limits = c(4, 5.5))

# 1.2 Table 2 ----
summary(
  lm(
    effective ~ 
      pos_treatment_ref, 
    data = ds
  )
)

summary(
  lm(
    effective ~ 
      pos_treatment_ref + partymatch, 
    data = ds
  )
)

summary(
  lm(
    effective ~ 
      pos_treatment_ref + partymatch + partyr2 + 
      white + black + hisplat + asam + natam + rother + 
      poly(age, 2, raw = TRUE) + gender + edu + ideo + income, 
    data = ds
  )
)

summary(
  lm(
    money ~ 
      pos_treatment_ref, 
    data = ds
  )
)

summary(
  lm(
    money ~ 
      pos_treatment_ref + partymatch, 
    data = ds
  )
)

summary(
  lm(
    money ~ 
      pos_treatment_ref + partymatch + partyr2 + 
      white + black + hisplat + asam + natam + rother + 
      poly(age, 2, raw = TRUE) + gender + edu + ideo + income, 
    data = ds
  )
)

# 2.0 DV = MC Support ----
# 2.1 Figure 2 ----
ds %>% 
  drop_na(therm) %>%
  group_by(pos_treatment) %>% 
  summarise(
    nrow = length(therm), 
    mean = mean(therm), 
    se = sd(therm)/sqrt(nrow),
    ci = 1.96 * se,
    lower_ci = mean - ci,
    upper_ci = mean + ci
  ) %>% 
  ggplot(., aes(x = pos_treatment, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 1, 
    linewidth = 1
  ) + 
  labs(y = "Thermometer Rating (0-100)", title = "") +  
  scale_x_discrete(
    labels = c(
      "control" = "Control \n", 
      "nocom" = "No \nCommittee", 
      "commem" = "Subcommittee\nMember",
      "comlead" = "Subcommittee\nLeader"
    )
  ) + 
  scale_y_continuous(limits = c(55, 65))

ds %>% 
  drop_na(pleased) %>%
  group_by(pos_treatment) %>% 
  summarise(
    nrow = length(pleased), 
    mean = mean(pleased), 
    se = sd(pleased)/sqrt(nrow),
    ci = 1.96 * se,
    lower_ci = mean - ci,
    upper_ci = mean + ci
  ) %>% 
  ggplot(., aes(x = pos_treatment, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 1, 
    linewidth = 1
  ) + 
  labs(y = "Pleased Rating (1-7)", title = "") +  
  scale_x_discrete(
    labels = c(
      "control" = "Control \n", 
      "nocom" = "No \nCommittee", 
      "commem" = "Subcommittee\nMember",
      "comlead" = "Subcommittee\nLeader"
    )
  ) + 
  scale_y_continuous(limits = c(4, 5))

# 2.2 Table 3 ----
summary(
  lm(
    therm ~ 
      pos_treatment_ref, 
    data = ds
  )
)

summary(
  lm(
    therm ~ 
      pos_treatment_ref + partymatch, 
    data = ds
  )
)

summary(
  lm(
    therm ~ 
      pos_treatment_ref + partymatch + partyr2 + 
      white + black + hisplat + asam + natam + rother + 
      poly(age, 2, raw = TRUE) + gender + edu + ideo + income, 
    data = ds
  )
)

summary(
  lm(
    pleased ~ 
      pos_treatment_ref, 
    data = ds
  )
)

summary(
  lm(
    pleased ~ 
      pos_treatment_ref + partymatch, 
    data = ds
  )
)

summary(
  lm(
    pleased ~ 
      pos_treatment_ref + partymatch + partyr2 + 
      white + black + hisplat + asam + natam + rother + 
      poly(age, 2, raw = TRUE) + gender + edu + ideo + income, 
    data = ds
  )
)
